﻿// <copyright file="ProductIcon.cs" company="Snowman Consulting">
//     Copyright (c) 2009 Snowman Consulting. All rights reserved.
// </copyright>

namespace SnowmanConsulting.ProductKeyManager.Data
{
    using System;
    using System.ComponentModel;
    using System.Windows.Media.Imaging;

    /// <summary>
    /// Icon class
    /// </summary>
    public class ProductIcon : INotifyPropertyChanged
    {
        #region Fields

        /// <summary>
        /// Icon Url field
        /// </summary>
        private string iconUrl;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProductIcon"/> class.
        /// </summary>
        public ProductIcon()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProductIcon"/> class.
        /// </summary>
        /// <param name="iconurl">The iconurl.</param>
        internal ProductIcon(string iconurl)
        {
            this.IconUrl = iconurl;
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the icon URL.
        /// </summary>
        /// <value>The icon URL.</value>
        public string IconUrl
        {
            get
            {
                return this.iconUrl;
            }

            set
            {
                if (this.iconUrl != value)
                {
                    this.iconUrl = value;
                    this.NotifyPropertyChanged("IconUrl");
                    this.NotifyPropertyChanged("Image");
                }
            }
        }

        /// <summary>
        /// Gets the image.
        /// </summary>
        /// <value>The image.</value>
        public BitmapImage Image
        {
            get
            {
                return ProductIconHelper.LoadImage(new Uri(this.IconUrl, UriKind.RelativeOrAbsolute));
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.IconUrl;
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return this.IconUrl.GetHashCode();
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            ProductIcon objIcon = obj as ProductIcon;

            if (objIcon == null)
            {
                return false;
            }

            if (objIcon.GetHashCode() == this.GetHashCode())
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Notifies the property changed.
        /// </summary>
        /// <param name="info">The name of the property changed</param>
        private void NotifyPropertyChanged(string info)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}
